package com.lagou.module4.work03;

public class Ticket {

    private int num ;

    public Ticket(int num){
        super();
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public synchronized void saleTickets(){

        if(num>0){
            System.out.println(Thread.currentThread().getName() + "卖出一张票，剩余" + (getNum()-1) );
            num--;
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("票已售空");
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

